package com.ruoyi.factorydata.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.factorydata.domain.CraftProcessRelation;
import com.ruoyi.factorydata.domain.dto.CraftRouteDTO;
import com.ruoyi.factorydata.domain.vo.CraftRouteVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.factorydata.domain.CraftRoute;
import com.ruoyi.factorydata.service.ICraftRouteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工艺路线Controller
 *
 * @author ruoyi
 * @date 2025-07-15
 */
@RestController
@RequestMapping("/craftroute/craftroute")
public class CraftRouteController extends BaseController {
    @Autowired
    private ICraftRouteService craftRouteService;

    /**
     * 查询工艺路线列表
     */
    @PreAuthorize("@ss.hasPermi('craftroute:craftroute:list')")
    @GetMapping("/list")
    public TableDataInfo list(CraftRoute craftRoute) {
        startPage();
        List<CraftRouteVo> list = craftRouteService.selectCraftRouteList(craftRoute);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(new PageInfo(list).getTotal());
        return tableDataInfo;
    }

    /**
     * 导出工艺路线列表
     */
    @PreAuthorize("@ss.hasPermi('craftroute:craftroute:export')")
    @Log(title = "工艺路线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CraftRoute craftRoute) {
        List<CraftRouteVo> list = craftRouteService.selectCraftRouteList(craftRoute);
        ExcelUtil<CraftRouteVo> util = new ExcelUtil<CraftRouteVo>(CraftRouteVo.class);
        util.exportExcel(response, list, "工艺路线数据");
    }

    /**
     * 获取工艺路线详细信息
     */
    @PreAuthorize("@ss.hasPermi('craftroute:craftroute:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(craftRouteService.selectCraftRouteById(id));
    }

    /**
     * 获取工艺相关详细信息
     */
//    @PostMapping(value = "/getInfoByCraftProcessRelation")
//    public AjaxResult getInfoByCraftProcessRelation(@RequestBody CraftProcessRelation craftProcessRelation){
//
//        return success(craftRouteService.selectByCraftProcessRelation(craftProcessRelation));
//    }
    @GetMapping(value = "/getInfoByIdsOrProcessIds")
    public AjaxResult getInfoByIdsOrProcessIds(@RequestParam(value = "ids", required = false) String ids, @RequestParam(value = "processIds", required = false) String processIds) {
        List<Long> paramsIds = new ArrayList<>();
        if (ids != null) {
            paramsIds = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        }
        List<Long> paramsProcessIds = new ArrayList<>();
        if (processIds != null) {
            paramsProcessIds = Arrays.stream(processIds.split(",")).map(Long::parseLong).collect(Collectors.toList());
        }
        return success(craftRouteService.selectByIdsOrProcessIds(paramsIds, paramsProcessIds));
    }

    /**
     * 新增工艺路线
     */
    @PreAuthorize("@ss.hasPermi('craftroute:craftroute:add')")
    @Log(title = "工艺路线", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CraftRouteDTO craftRouteDTO) {
        boolean exist = craftRouteService.lambdaQuery().eq(craftRouteDTO.getRouteCode() != null, CraftRoute::getRouteCode, craftRouteDTO.getRouteCode()).count() > 0;
        if (exist) {
            return warn("工艺路线已存在");
        }
        craftRouteDTO.setCreateBy(getUserId().toString());
        return toAjax(craftRouteService.insertCraftRoute(craftRouteDTO));
    }

    /**
     * 修改工艺路线
     */
    @PreAuthorize("@ss.hasPermi('craftroute:craftroute:edit')")
    @Log(title = "工艺路线", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CraftRouteDTO craftRouteDTO) {
        boolean exists = craftRouteService.lambdaQuery()
                .ne(CraftRoute::getId, craftRouteDTO.getId())
                .eq(CraftRoute::getRouteCode, craftRouteDTO.getRouteCode())
                .count() > 0;
        if (exists) {
            return warn("工艺路线已存在");
        }
        return toAjax(craftRouteService.updateCraftRoute(craftRouteDTO));
    }

    @Log(title = "工艺路线修改标签", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('craftroute:craftroute:updateTag')")
    @PutMapping("/updateTag")
    public AjaxResult updateTag(@RequestBody CraftRouteDTO craftRouteDTO) {
        boolean update = craftRouteService.update(new UpdateWrapper<CraftRoute>().lambda()
                .eq(CraftRoute::getId, craftRouteDTO.getId())
                .set(CraftRoute::getCraftTag, craftRouteDTO.getCraftTag()));
        return toAjax(update);
    }

    @PreAuthorize("@ss.hasPermi('craftroute:craftroute:updateStatus')")
    @Log(title = "工艺路线修改状态", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody CraftRouteDTO craftRouteDTO) {
        CraftRoute craftRoute = new CraftRoute();
        craftRoute.setId(craftRouteDTO.getId());
        craftRoute.setStatus(craftRouteDTO.getStatus());
        craftRoute.setUpdateTime(DateUtils.getNowDate());
        boolean update = craftRouteService.update(new UpdateWrapper<CraftRoute>().lambda()
                .set(CraftRoute::getUpdateTime, craftRoute.getUpdateTime())
                .set(CraftRoute::getStatus, craftRoute.getStatus())
                .eq(CraftRoute::getId, craftRoute.getId()));
        return toAjax(update);
    }

    /**
     * 删除工艺路线
     */
    @PreAuthorize("@ss.hasPermi('craftroute:craftroute:remove')")
    @Log(title = "工艺路线", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(craftRouteService.deleteCraftRouteByIds(ids));
    }
}
